package org.zkdn.modules.wlpt.basicinfo.controller;

import java.util.*;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.BeanUtils;
import org.zkdn.modules.wlpt.basicinfo.dto.FileDataModel;
import org.zkdn.modules.wlpt.basicinfo.dto.RoadsidePoleDTO;
import org.zkdn.modules.wlpt.basicinfo.entity.RoadsidePole;
import org.zkdn.modules.wlpt.basicinfo.entity.TCrossing;
import org.zkdn.modules.wlpt.basicinfo.entity.TRoadRefCross;
import org.zkdn.modules.wlpt.basicinfo.service.IRoadsidePoleService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.zkdn.modules.wlpt.basicinfo.vo.RoadsidePoleVO;

/**
 * @Description: roadside_pole
 * @Author: jeecg-boot
 * @Date:   2025-06-18
 * @Version: V1.0
 */
@Api(tags="roadside_pole")
@RestController
@RequestMapping("/basicinfo/roadside/pole")
@Slf4j
public class RoadsidePoleController extends JeecgController<RoadsidePole, IRoadsidePoleService> {
	@Autowired
	private IRoadsidePoleService roadsidePoleService;

	 @RequestMapping(value = "/queryPoleWithPositionList", method = RequestMethod.GET)
	 public Result<List<RoadsidePoleVO>> queryPoleWithPositionList(@RequestParam(name="crossId", required=false) Integer crossId,
											@RequestParam(name="roadId", required=false) String roadId) {
		 Result<List<RoadsidePoleVO>> result = new Result<>();
		 QueryWrapper<RoadsidePole> queryWrapper = new QueryWrapper<>();
		 Optional.ofNullable(crossId).ifPresent(crossid -> queryWrapper.eq("t1.cross_id", crossid));
		 Optional.ofNullable(roadId).ifPresent(roadid -> queryWrapper.eq("t1.road_id", roadid));
		 List<RoadsidePoleVO> list = roadsidePoleService.queryPoleWithPositionList(queryWrapper);
		 if(list==null||list.size()<=0) {
			 result.error500("未找到路侧杆件信息");
		 }else {
			 result.setResult(list);
			 result.setSuccess(true);
		 }
		 return result;
	 }
	/**
	 * 分页列表查询
	 *
	 * @param roadsidePole
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "roadside_pole-分页列表查询")
	@ApiOperation(value="roadside_pole-分页列表查询", notes="roadside_pole-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<RoadsidePoleVO>> queryPageList(RoadsidePole roadsidePole,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<RoadsidePole> queryWrapper = QueryGenerator.initQueryWrapper(roadsidePole, req.getParameterMap());
		Page<RoadsidePoleVO> page = new Page<>(pageNo, pageSize);
		IPage<RoadsidePoleVO> pageList = roadsidePoleService.queryPageList(page, queryWrapper);
		// 创建TCrossingRefTag集合
		pageList.getRecords().stream().forEach(roadsidePoleVO -> {
				String urls = roadsidePoleVO.getImagePath();
				String imageNames = roadsidePoleVO.getImageName();
				if(StringUtils.isNotEmpty(urls)) {
					List<String> urlList = Arrays.asList(urls.split(","));
					List<String> fileNameList = Arrays.asList(imageNames.split(","));
					List<FileDataModel> fileDataModels = new ArrayList();
					for (int i = 0; i < urlList.size(); i++) {
						FileDataModel fileDataModel = new FileDataModel();
						fileDataModel.setUrl(urlList.get(i));
						fileDataModel.setName(fileNameList.get(i));
						fileDataModels.add(fileDataModel);
					}
					roadsidePoleVO.setFileList(fileDataModels);
				}
			});
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param roadsidePoleDTO
	 * @return
	 */
	@AutoLog(value = "roadside_pole-添加")
	@ApiOperation(value="roadside_pole-添加", notes="roadside_pole-添加")
	//@RequiresPermissions("org.zkdn.modules.wlpt:roadside_pole:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody RoadsidePoleDTO roadsidePoleDTO) {
		RoadsidePole roadsidePole = new RoadsidePole();
		BeanUtils.copyProperties(roadsidePoleDTO, roadsidePole);
		//处理图片文件
		List<FileDataModel> fileDataModels = roadsidePoleDTO.getFileList();
		String urls = fileDataModels.stream()
				.map(FileDataModel::getUrl)
				.collect(Collectors.joining(","));
		String uids = fileDataModels.stream()
				.map(FileDataModel::getUid)
				.collect(Collectors.joining(","));
		String names = fileDataModels.stream()
				.map(FileDataModel::getName)
				.collect(Collectors.joining(","));
		roadsidePole.setImagePath(urls);
		roadsidePole.setImageName(names);
		//保存
		return roadsidePoleService.saveByValidate(roadsidePole);
	}

	/**
	 *  编辑
	 *
	 * @param roadsidePoleDTO
	 * @return
	 */
	@AutoLog(value = "roadside_pole-编辑")
	@ApiOperation(value="roadside_pole-编辑", notes="roadside_pole-编辑")
	//@RequiresPermissions("org.zkdn.modules.wlpt:roadside_pole:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody RoadsidePoleDTO roadsidePoleDTO) {
		RoadsidePole roadsidePole = new RoadsidePole();
		BeanUtils.copyProperties(roadsidePoleDTO, roadsidePole);

		//处理图片文件
		List<FileDataModel> fileDataModels = roadsidePoleDTO.getFileList();
		String urls = fileDataModels.stream()
				.map(FileDataModel::getUrl)
				.collect(Collectors.joining(","));
		String uids = fileDataModels.stream()
				.map(FileDataModel::getUid)
				.collect(Collectors.joining(","));
		String names = fileDataModels.stream()
				.map(FileDataModel::getName)
				.collect(Collectors.joining(","));
		roadsidePole.setImagePath(urls);
		roadsidePole.setImageName(names);
		//更新
		return roadsidePoleService.updateByValidate(roadsidePole);
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "roadside_pole-通过id删除")
	@ApiOperation(value="roadside_pole-通过id删除", notes="roadside_pole-通过id删除")
	//@RequiresPermissions("org.zkdn.modules.wlpt:roadside_pole:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		return roadsidePoleService.deleteById(id);
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "roadside_pole-批量删除")
	@ApiOperation(value="roadside_pole-批量删除", notes="roadside_pole-批量删除")
	//@RequiresPermissions("org.zkdn.modules.wlpt:roadside_pole:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		return roadsidePoleService.deleteByIds(ids);
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "roadside_pole-通过id查询")
	@ApiOperation(value="roadside_pole-通过id查询", notes="roadside_pole-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<RoadsidePole> queryById(@RequestParam(name="id",required=true) String id) {
		RoadsidePole roadsidePole = roadsidePoleService.getById(id);
		if(roadsidePole==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(roadsidePole);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param roadsidePole
    */
    //@RequiresPermissions("org.zkdn.modules.wlpt:roadside_pole:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, RoadsidePole roadsidePole) {
        return super.exportXls(request, roadsidePole, RoadsidePole.class, "roadside_pole");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("roadside_pole:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, RoadsidePole.class);
    }

}
